package at.ac.univie.zsu.aguataplan.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import at.ac.univie.zsu.aguataplan.dao.Dao;
import at.ac.univie.zsu.aguataplan.dao.EventDao;
import at.ac.univie.zsu.aguataplan.domain.Event;
import at.ac.univie.zsu.aguataplan.domain.EventAttribute;
import at.ac.univie.zsu.aguataplan.domain.EventAttributeValueString;
import at.ac.univie.zsu.aguataplan.exception.DatabaseException;

/**
 * @author gerry
 * 
 */
@Service
public class EventManagerBean implements EventManager {
	private static Logger log = Logger.getLogger(EventManagerBean.class);

	@Autowired
	private EventDao eventDao;

	@Autowired
	private Dao dao;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public EventManagerBean() {

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	@Override
	public void cleanEventEventAttributeEventAttributeValueString() throws DatabaseException {
		try {
			eventDao.cleanEventEventAttributeEventAttributeValueString();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entries", e.getMessage());
		}
	}

	// --------------

	@Override
	public Event addEvent(String name, String description, String shortcut) throws DatabaseException {
		Event event = new Event();
		event.setName(name);
		event.setDescription(description);
		event.setShortcut(shortcut);

		Event retEvent;
		try {
			retEvent = eventDao.addEvent(event);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not insert entry", e.getMessage());
		}
		return retEvent;
	}

	@Override
	public void deleteEvent(long id) throws DatabaseException {
		try {
			eventDao.deleteEvent(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entry", e.getMessage());
		}
	}

	@Override
	public List<Event> getEventList() throws DatabaseException {
		List<Event> list = null;

		try {
			list = eventDao.getEventList();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}
		return list;
	}

	@Override
	public Event getEventById(long id) throws DatabaseException {
		Event retEvent = null;
		try {
			retEvent = eventDao.getEventById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entry", e.getMessage());
		}
		return retEvent;
	}

	@Override
	public Event mergeEvent(Event event) throws DatabaseException {
		Event retEvent = null;
		try {
			retEvent = eventDao.mergeEvent(event);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not save entry", e.getMessage());
		}
		return retEvent;
	}

	@Override
	public List<Event> getEventListNonEmpty() throws DatabaseException {
		List<Event> list = null;

		try {
			list = eventDao.getEventListNonEmpty();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}
		return list;
	}

	@Override
	public List<Event> getEventListNonEmptyAll() throws DatabaseException {
		List<Event> list = null;

		list = getEventListNonEmpty();

		if (list.size() == 0) {
			list = new ArrayList<Event>();
			list.add(null);
		} else {
			list.add(0, null);
		}

		return list;
	}

	// --------------

	@Override
	public EventAttribute addEventAttribute(String name, String shortcut) throws DatabaseException {
		EventAttribute eventAttribute = new EventAttribute();
		eventAttribute.setName(name);
		eventAttribute.setShortcut(shortcut);

		EventAttribute retEventAttribute;
		try {
			retEventAttribute = eventDao.addEventAttribute(eventAttribute);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not insert entry", e.getMessage());
		}
		return retEventAttribute;
	}

	@Override
	public void deleteEventAttribute(long id) throws DatabaseException {
		try {
			eventDao.deleteEventAttribute(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entry", e.getMessage());
		}
	}

	@Override
	public List<EventAttribute> getEventAttributeList() throws DatabaseException {
		List<EventAttribute> list = null;

		try {
			list = eventDao.getEventAttributeList();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}
		return list;
	}

	@Override
	public EventAttribute getEventAttributeById(long id) throws DatabaseException {
		EventAttribute retEventAttribute = null;
		try {
			retEventAttribute = eventDao.getEventAttributeById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entry", e.getMessage());
		}
		return retEventAttribute;
	}

	@Override
	public EventAttribute mergeEventAttribute(EventAttribute attribute) throws DatabaseException {
		EventAttribute retEventAttribute = null;
		try {
			retEventAttribute = eventDao.mergeEventAttribute(attribute);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not save entry", e.getMessage());
		}
		return retEventAttribute;
	}

	@Override
	public List<EventAttribute> getEventAttributeListByEventNonEmpty(Event event) throws DatabaseException {
		List<EventAttribute> list = null;

		try {
			list = eventDao.getEventAttributeListByEventNonEmpty(event);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}
		return list;
	}

	// --------------

	@Override
	public EventAttributeValueString addEventAttributeValueString(String attValue) throws DatabaseException {
		EventAttributeValueString eventAttributeValueString = new EventAttributeValueString();
		eventAttributeValueString.setAttValue(attValue);

		EventAttributeValueString retEventAttributeValueString;
		try {
			retEventAttributeValueString = eventDao.addEventAttributeValueString(eventAttributeValueString);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not insert entry", e.getMessage());
		}
		return retEventAttributeValueString;
	}

	@Override
	public void deleteEventAttributeValueString(long id) throws DatabaseException {
		try {
			eventDao.deleteEventAttributeValueString(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entry", e.getMessage());
		}
	}

	@Override
	public List<EventAttributeValueString> getEventAttributeValueStringList() throws DatabaseException {
		List<EventAttributeValueString> list = null;

		try {
			list = eventDao.getEventAttributeValueStringList();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}
		return list;
	}

	@Override
	public EventAttributeValueString getEventAttributeValueStringById(long id) throws DatabaseException {
		EventAttributeValueString retEventAttributeValueString = null;
		try {
			retEventAttributeValueString = eventDao.getEventAttributeValueStringById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entry", e.getMessage());
		}
		return retEventAttributeValueString;
	}

	@Override
	public EventAttributeValueString mergeEventAttributeValueString(EventAttributeValueString eventAttributeValueString)
			throws DatabaseException {
		EventAttributeValueString retEventAttributeValueString = null;
		try {
			retEventAttributeValueString = eventDao.mergeEventAttributeValueString(eventAttributeValueString);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not save entry", e.getMessage());
		}
		return retEventAttributeValueString;
	}

	@Override
	public List<EventAttributeValueString> getEventAttributeValueStringListByEventAttribute(
			EventAttribute eventAttribute) throws DatabaseException {
		List<EventAttributeValueString> list = null;

		try {
			list = eventDao.getEventAttributeValueStringListByEventAttribute(eventAttribute);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}
		return list;
	}

	// --------------

	@Override
	public void addEventAttributeToEvent(Event event, EventAttribute eventAttribute) throws DatabaseException {

		try {
			eventDao.addEventEventAttribute(event, eventAttribute);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not save entry", e.getMessage());
		}

	}

	@Override
	public void deleteEventAttributeFromEvent(Event event, EventAttribute eventAttribute) throws DatabaseException {
		try {
			eventDao.deleteEventEventAttribute(event, eventAttribute);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entry", e.getMessage());
		}

		// Event ev = null;
		// EventAttribute ea = null;
		// try {
		// // ev = dao.find(Event.class, event.getId());
		// // ea = dao.find(EventAttribute.class, eventAttribute.getId());
		// ev = eventDao.getEventById(event.getId());
		// ea = eventDao.getEventAttributeById(eventAttribute.getId());
		// } catch (Exception e) {
		// log.error(e.getMessage());
		// throw new DatabaseException("could not find entry", e.getMessage());
		// }
		//
		// // TODO: lookup is a linear lookup but not a hash
		// if (!ev.deleteEventAttribute(ea)) {
		// throw new DatabaseException("could not save entry",
		// "could not delete eventAttribute");
		// }
		// if (!ea.deleteEvent(ev)) {
		// throw new DatabaseException("could not save entry",
		// "could not delete event");
		// }
		//
		// try {
		// dao.merge(ev);
		// dao.merge(ea);
		// } catch (Exception e) {
		// log.error(e.getMessage());
		// throw new DatabaseException("could not save entry", e.getMessage());
		// }
	}

	@Override
	public void addEventAttributeValueStringtoEventAttribute(EventAttribute eventAttribute,
			EventAttributeValueString eventAttributeValueString) throws DatabaseException {

		try {
			eventDao.addEventAttributeEventAttributeValueString(eventAttribute, eventAttributeValueString);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not save entry", e.getMessage());
		}

	}

	@Override
	public void deleteEventAttributeValueStringFromEventAttribute(EventAttribute eventAttribute,
			EventAttributeValueString eventAttributeValueString) throws DatabaseException {

		try {
			eventDao.deleteEventAttributeEventAttributeValueString(eventAttribute, eventAttributeValueString);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entry", e.getMessage());
		}

	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public EventDao getEventDao() {
		return eventDao;
	}

	public void setEventDao(EventDao eventDao) {
		this.eventDao = eventDao;
	}

	public Dao getDao() {
		return dao;
	}

	public void setDao(Dao dao) {
		this.dao = dao;
	}

}
